package org.wutopia.labcloud.domain.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserAddressCreateInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserAddressGetListOutputVO;
import org.wutopia.labcloud.domain.uaa.dao.UserAddressDao;
import org.wutopia.labcloud.domain.uaa.dao.entity.UserAddress;
import org.wutopia.labcloud.domain.uaa.service.UserAddressService;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
@AllArgsConstructor
public class UserAddressServiceImpl implements UserAddressService {


    private UserAddressDao userAddressDao;

    @Override
    public List<UserAddressGetListOutputVO> getLIst(Long userId) {

        QueryWrapper<UserAddress> queryWrapper  = new QueryWrapper<>();

        queryWrapper.eq("user_id", userId);
        List<UserAddress> userAddresses = userAddressDao.selectList(queryWrapper);



        List<UserAddressGetListOutputVO> list = new ArrayList<>();
        for(UserAddress ua: userAddresses) {

            UserAddressGetListOutputVO output = new UserAddressGetListOutputVO();

            BeanUtils.copyProperties(ua, output);

            list.add(output);

        }


        return list;
    }

    @Override
    public void create(UserAddressCreateInputVO vo) {

        UserAddress userAddress = new UserAddress();


        BeanUtils.copyProperties(vo, userAddress);

        userAddressDao.insert(userAddress);

    }

    @Override
    public void delete(Long userAddrId) {
        userAddressDao.deleteById(userAddrId);
    }

}
